const express = require("express")
const router = express.Router()

const ejs = require("ejs")
const fs = require("fs")
const md5 = require("md5-node")
const multiparty = require("multiparty")

const session = require("express-session")

const DB = require("../../mongodb.js")
const bodyParser = require("body-parser")

const previous = require("./room/previous.js")
const next = require("./room/next.js")
const search = require("./room/search.js")

router.use('/previous',previous)

router.use('/next',next)

router.use('/search',search)

// /room====================================获取宿舍水电用量列表==============================================
router.get('/',function(request,response){
    // console.log(request.session)

    console.log(request.session.dbName)

    const name = request.body.Uname
    const building = request.body.Ubuilding
    const door = request.body.Udoor
    const balance = request.body.Ubalance

    const dbName = request.session.dbName

    const database = dbName ? dbName : 'weConsumption'
    console.log("now consoling "+database)

    // console.log(request.session.newdata)
    // const newdata = request.session.newdata
    // console.log(newdata.length)

    // console.log(newdata.parseJSON())
    console.log(request.query.pageIndex) // undefined
    console.log(request.session.pageIndex)
    const pageIndex = request.query.pageIndex ? request.query.pageIndex : request.session.pageIndex
    DB.find(database,{},function(error,data){   //building:building,door:door
        if(error){
            console.log(error)
            return false
        }else{
            console.log("weConsumption console success "+ data)
            response.render('room.html',{
                // list: newdata.length ? newdata : data
                list:data
            })
        }
    },pageIndex)
})

// /room====================================更新账户/余额==============================================
router.get('/balance',function(request,response){


    response.render('balance.html')
})

router.post('/balance',function(request,response){
    // console.log(request.session)
    console.log(request.body)


    //应该做成update会更加合适，之后改
    DB.update('weConsumption',({ 
        building : request.body.building,
        door : request.body.door}),{
        balance : balance+request.body.balance}
    ,function(error,result){
        if(error){
            console.log("余额添加失败")
            console.log(error)
            response.send("<script>alert('余额添加失败');location.href='/room'</script>")
            return false
        }else{
            console.log("充值成功")
            response.redirect('/room')
        }
    })
})



// /room====================================更新账户/余额==============================================

// router.get('/managerinfo',function(request,response){
//     response.render('managerInfo.html')
// })

router.get('/managerinfo',function(request,response){
    // console.log(request.session)

    console.log(request.session.dbName)

    const dbName = request.session.dbName

    const database = dbName ? dbName : 'Admin'
    console.log("now consoling "+database)

    console.log(request.query.pageIndex) // undefined
    console.log(request.session.pageIndex)
    const pageIndex = request.query.pageIndex ? request.query.pageIndex : request.session.pageIndex
    DB.find(database,{},function(error,data){
        if(error){
            console.log(error)
            return false
        }else{
            console.log("Admin console success "+ data)
            response.render('managerInfo.html',{
                // list: newdata.length ? newdata : data
                list:data
            })
        }
    },pageIndex)
})



module.exports = router